Plotly is a high-level interface to plotly.js, based on d3.js which provides an easy-to-use UI to generate slick D3 interactive graphics. These interactive graphs give the user the ability to zoom the plot in and out, hover over a point to get additional information, filter to groups of points, and much more. These interactive components contribute to an engaging user experience and allows information to be displayed in ways that are not possible with static figures.
There are two main approaches to initialize a plotly object: transforming a ggplot2 object with ggplotly() or setting up aesthetics mappings with plot_ly() directly.
ggplotly() takes existing ggplot2 objects and converts them into interactive plotly graphics. This makes it easy to create interactive figures while using the ggplot2 syntax that we are already used to. Additionally, ggplotly() allows us to use ggplot2 functionality that would not be as easily replicated with plotly and tap into the wide range of ggplot2 extension packages.
At this case, the plot is not interactive.
(ggplot_object <- mpg %>%
ggplot(aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth())
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
But we can transfer this plot to interactive plot by using plotly command, here is the example.
ggplotly(ggplot_object)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Plotly is a really important tool to plot the dataset in shinyapp!